<div class="layuimini-main">
    <div style="width:600px;margin:0px auto;">
        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label required">任务名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required" lay-reqtext="任务名不能为空" placeholder="请输入任务名称" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">定时规则</label>
                <div class="layui-input-block">
                    <input type="text" name="rule" lay-verify="required" lay-reqtext="定时规则不能为空" placeholder="* * * * * *" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">执行类型</label>
                <div class="layui-input-block">
                    <select name="type" lay-filter="type" lay-verify="required" onchange="">
                        <option value=""></option>
                        {foreach $types as $k=> $v}
                            <option value="{$k}">{$v}</option>
                        {/foreach}
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">执行命令</label>
                <div class="layui-input-block">
                    <input type="text" name="command" lay-verify="required" lay-reqtext="执行命令不能为空" placeholder="请输入执行命令" value="" class="layui-input">
                    <tip style='color:red'>如果为执行类型为http请求，请输入请求地址</tip>
                </div>

            </div>

            <div class="layui-form-item" style='display:none'>
                <label class="layui-form-label required" >请求方式</label>
                <div class="layui-input-block">
                    <input type="radio" name="http_method" value="1" title="post" checked="">
                    <input type="radio" name="http_method" value="2" title="get">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">命令参数</label>
                <div class="layui-input-block">
                    <input type="text" name="params"  placeholder="命令参数" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">超时时间(s)</label>
                <div class="layui-input-block">
                    <input type="number" name="time_out" lay-verify="required" lay-reqtext="超时时间不能为空" placeholder="请输入超时时间(秒)" value="10" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注信息</label>
                <div class="layui-input-block">
                    <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
                </div>
            </div>

             <div class="layui-form-item">
                <label class="layui-form-label required">状态</label>
                <div class="layui-input-block">
                    <input type="radio" name="status" value="2" title="开启" checked="">
                    <input type="radio" name="status" value="0" title="关闭">    
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>

    </div>
</div>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();
        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.load(1, {shade: [0.1,'#fff']});
            let field = data.field;
            let url = "/admin/crontab/add";
            $.ajax({
                type: "POST",
                url: url,
                data: field,
                dataType: "json",
                success: function(data){
                    layer.close(index);
                    if(data.code == 1){
                        layer.alert(data.msg,{icon:1},function(index){
                            layer.closeAll();
                        });
                        parent.layui.table.reload('currentTableId');
                    }else{
                        layer.alert(data.msg,{icon:2});
                    }
                },
                error:function(){
                    layer.close(index);
                    layer.alert('网络错误啦');
                }
            });
            return false;
        });
        //下拉框监听事件
        form.on('select(type)', function(data){   
            var val=data.value;
            if(val == 'Http'){
                $('#http_method').show();
            }else{
                $('#http_method').hide();
            }
        });
    });
</script>